package com.tsd.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.tsd.core.cache.CacheOperatorApi;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.PageBean;
import com.tsd.service.BaseServiceImpl;
import com.tsd.system.dao.SysDataAuthConfigMapper;
import com.tsd.system.entity.SysDataAuthConfig;
import com.tsd.system.entity.SysDataAuthConfigExt;
import com.tsd.system.entity.SysDataAuthExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysDataAuthConfigService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysDataAuthConfig 服务实现层
 *
 * @author admin
 * @date 2022/11/15 15:12:44
 */
@Service
public class SysDataAuthConfigServiceImpl extends BaseServiceImpl implements SysDataAuthConfigService {

    @Resource
    private SysDataAuthConfigMapper sysDataAuthConfigMapper;
    @Resource
    private CacheOperatorApi<List<SysDataAuthExt>> sysDataAuthCacheOperatorApi;

    @Override
    public SysDataAuthConfig getSysDataAuthConfigById(Long id) throws Exception {
        return sysDataAuthConfigMapper.selectByPrimaryKey(id);
    }

    @Override
    public SysDataAuthConfigExt queryBySid(String sid) throws Exception {
        return sysDataAuthConfigMapper.selectBySid(sid);
    }

    @Override
    public void deleteSysDataAuthConfigs(List<String> list, SysUser opUser) throws Exception {
        for (String sid : list) {
            SysDataAuthConfig old = sysDataAuthConfigMapper.selectBySid(sid);
            super.checkEmpty(old, "该记录不存在");
            super.autoInjectBaseData(old, opUser, TYPE_DELETE);
            sysDataAuthConfigMapper.updateByPrimaryKeySelective(old);
        }
    }

    @Override
    public void saveSysDataAuthConfig(SysDataAuthConfig record, SysUser opUser) throws Exception {
        if (HlpUtils.isEmpty(record.getId())) {
            super.autoInjectBaseData(record, opUser, this.TYPE_CREATE);
            sysDataAuthConfigMapper.insertSelective(record);
        } else {
            super.autoInjectBaseData(record, opUser, this.TYPE_UPDATE);
            sysDataAuthConfigMapper.updateByPrimaryKeySelective(record);
        }
        this.clearCache();
    }

    private void clearCache() {
        Collection<String> keyList = sysDataAuthCacheOperatorApi.getAllKeys();
        if (!CollectionUtil.isEmpty(keyList)) {
            sysDataAuthCacheOperatorApi.remove(new ArrayList<>(keyList));
        }
    }

    @Override
    public List<SysDataAuthConfigExt> findSysDataAuthConfigs(Map<String, Object> params, PageBean page) throws Exception {
        return sysDataAuthConfigMapper.selectByPage(params, page);
    }

    @Override
    public List<SysDataAuthConfigExt> findSysDataAuthConfigs(Map<String, Object> params) throws Exception {
        return sysDataAuthConfigMapper.selectByParams(params);
    }
}